package com.ae.shield.common.items.shieldFittings;

import com.ae.shield.AEShieldMod;
import com.ae.shield.common.enchantment.EnchantmentWrap;
import com.ae.shield.common.enchantment.shieldSuffix.SuffixBase;
import com.ae.shield.common.items.util.ItemNBTHelper;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public class SuffixItem extends Item {
    public SuffixItem() {
        super(new Properties().group(AEShieldMod.AE_SHIELD));
    }

    @Override
    public void addInformation(@Nonnull ItemStack stack, @Nullable World world, List<ITextComponent> tooltip, @Nonnull ITooltipFlag flag) {
        ArrayList<EnchantmentWrap> ens = readSuffix(stack);
        tooltip.add(new TranslationTextComponent("tooltip.ae.shield.suffix").mergeStyle(TextFormatting.GRAY));
        if (ens != null && !ens.isEmpty()) {
            for (EnchantmentWrap en : ens) {
                tooltip.add(new TranslationTextComponent("enchantment." + String.valueOf(en.getEn().getRegistryName()).replace(":", "."))
                        .append(new StringTextComponent(" " + en.getLvl())
                                .append(new TranslationTextComponent("tooltip.ae.shield.level"))).mergeStyle(TextFormatting.BLUE));
            }
        } else {
            tooltip.add(new TranslationTextComponent("tooltip.ae.shield.empty").mergeStyle(TextFormatting.GRAY));
        }
    }

    /**
     * @param ens 包含后缀以及后缀等级的ArrayList<EnchantmentWrap>
     * @param stack     一个需要添加后缀的SuffixItem
     */
    public void writeSuffix(ArrayList<EnchantmentWrap> ens, ItemStack stack) {
        ListNBT listnbt = new ListNBT();

        for (EnchantmentWrap en : ens) {
            Enchantment enchantment = en.getEn();
            if (enchantment != null) {
                int i = en.getLvl();
                CompoundNBT compoundnbt = new CompoundNBT();
                compoundnbt.putString("id", String.valueOf(ForgeRegistries.ENCHANTMENTS.getKey(enchantment)));
                compoundnbt.putShort("lvl", (short) i);
                listnbt.add(compoundnbt);
            }
        }

        ItemNBTHelper.setNBT(stack, "Suffixes", listnbt);
    }

    /**
     * @return 返回所保存的所有后缀
     */
    @Nullable
    public ArrayList<EnchantmentWrap> readSuffix(ItemStack stack) {
        if (!(stack.getItem() instanceof SuffixItem)) {
            return null;
        }

        ListNBT serialized = ItemNBTHelper.getList(stack, "Suffixes", 10, new ListNBT());
        ArrayList<EnchantmentWrap> ens = new ArrayList<>();

        for (int i = 0; i < serialized.size(); ++i) {
            CompoundNBT compoundNBT = serialized.getCompound(i);
            Enchantment suffix = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(compoundNBT.getString("id")));
            if (suffix instanceof SuffixBase) {
                ens.add(EnchantmentWrap.wrap(suffix, compoundNBT.getInt("lvl")));
            }
        }

        return ens;
    }
}
